package com.baidu.controller;

import com.baidu.entity.User;
import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;


/**
 * @author xiao
 */
@Controller
@RequestMapping("/users")
public class UserController {



    /**
     * 登录方法（属性传参）
     */
    @RequestMapping(value = "/login2",method = RequestMethod.POST)
    public String login(User user, HttpServletResponse response, HttpServletRequest request) {
        System.out.println(user + "+++++++++++++++++++++++++++++++++++++++++++");
        request.getSession().setAttribute("user", user);
        return "index1";
    }

    /**
     * 用户登录
     *
     * @param user
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user, Model model, HttpSession session) {
        //获取用户名和密码
        String username = user.getUname();
        String password = user.getUpwd();
        //些处横板从数据库中获取对用户名和密码后进行判断
        if (username != null && username.equals("admin") && password != null && password.equals("admin")){
            //将用户对象添加到Session中
            session.setAttribute("user", user);
            //重定向到主页面的跳转方法
            return "index1";
        }
        model.addAttribute("msg", "用户名或密码错误，请重新登录！");
        return "index";
    }

    /**
     * 上传文件到指定路径
     * @param uploadFile
     * @param request
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @RequestMapping("/uploadImg1")
    public String uploadImg(MultipartFile uploadFile, HttpServletRequest request) throws IllegalStateException, IOException {
        //解决文件名重复
        String uuid=UUID.randomUUID().toString();
       String sub=uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf("."));
       String fileName=uuid+sub;
       File file=new File("E:/Test");
       if(!file.exists()){
           file.mkdir();
       }
       File file1=new File(file,fileName);
       uploadFile.transferTo(file1);
        return "index1";

    }

    /**
     * 多文件上传到当前项目
     * @param uploadFile
     * @param session
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @RequestMapping("/uploadImg")
    public String uploadImg2(MultipartFile uploadFile, HttpSession session) throws IllegalStateException, IOException {
      String filename=uploadFile.getOriginalFilename();
      String lePath=session.getServletContext().getRealPath("/text");
      File file=new File(lePath,filename);
      uploadFile.transferTo(file);
        return "index1";
    }

    /**
     * 下载指定文件
     * @return
     * @throws IOException
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        File file=new File("C:\\Users\\Ler\\Pictures\\Screenshots\\404.png");
        HttpHeaders headers=new HttpHeaders();
        String filename=new String("你好.png".getBytes("UTF-8"),"iso-8859-1");
        headers.setContentDispositionFormData("attachment",filename);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        //清除session
        session.invalidate();
        //重定向到登录页面的跳转方法
        return "redirect:login";
    }
}
